package com.zoro.barn.commons.webcommons.response.entity;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zoro.barn.commons.foundation.web.ResponseInterface;
import com.zoro.barn.commons.tools.json.JsonUtil;
import com.zoro.barn.commons.foundation.constants.web.ResponseConstants;

/**
 * 统一响应值格式
 *
 * @author zhaoxingwu
 */
public class BarnResponseEntity<T> implements ResponseInterface<T> {

    /**
     * @see ResponseConstants#STATUS
     */
    private String status;
    /**
     * @see ResponseConstants#MESSAGE
     */
    private String message;
    /**
     * @see ResponseConstants#DATA
     */
    private T data;
    /**
     * @see ResponseConstants#EXCEPTION
     */
    private String exception;

    public BarnResponseEntity(String status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public BarnResponseEntity(T data) {
        this.status = ResponseConstants.STATUS_OK;
        this.message = null;
        this.data = data;
    }

    public BarnResponseEntity() {
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public <TT> TT getTargetData(Class<TT> ttClass) throws JsonProcessingException {
        return JsonUtil.toJavaType(JsonUtil.toString(data), ttClass);
    }

    @Override
    public String toString() {
        return "BarnResponseEntity{" +
                "status='" + status + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", exception='" + exception + '\'' +
                '}';
    }
}
